//1.引入 axios
import axios from 'axios'; 
import redux from 'redux/index';


//2.创建axios 实例
const service = axios.create({
    //3.配置baseURL 
    baseURL: 'http://localhost:3006',  
    // baseURL: 'http://132.232.87.95:3006',  
    timeout: 10 * 60 * 1000 //10分钟  
})

//4. 请求拦截
service.interceptors.request.use(
    (config) => {
        // 获取store
        let store = redux.getState();
        console.log(store);
        // store.token获取保存的token的值
        config.headers['user-token'] = store.user.token;
        return config;
    },
    (err) => {
        return Promise.reject(err);
    }
)

//5.响应拦截
service.interceptors.response.use(
    (res) => {
        const data = res.data; 
        if (data.code != 666) { 
			// console.log(data.msg);
            return Promise.reject(data.msg);
        }
        return data;
    },
    (err) => {
        return Promise.reject(err);
    }
)


const get = (url,data={})=> {
    return service.get(url,{params: data});
}
const post = (url,data={})=> {
    return service.post(url,data);
}

//6.导出
export default {
    get,post
};